package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.domain.MarketCategory;
import com.cskaoyan.market.db.domain.MarketCategoryExample;
import com.cskaoyan.market.db.mapper.MarketCategoryMapper;
import com.cskaoyan.market.service.MarketCategoryService;
import com.cskaoyan.market.util.MyBatisUtil;
import com.cskaoyan.market.vo.CategoryUpdateVo;
import com.cskaoyan.market.vo.CategoryVo;
import com.cskaoyan.market.vo.LabelValueVo;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@Service
public class MarketCategoryServiceImpl implements MarketCategoryService {
    @Autowired
    MarketCategoryMapper mapper;
    @Override
    public List<CategoryVo> list() {
        //最终返回的list
        List<CategoryVo> categoryVoList = new ArrayList<>();
        //从数据库中查询所有category条目
        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = marketCategoryExample.or();
        criteria.andLogicalDeleted(true);
        List<MarketCategory> categoryList = mapper.selectByExample(marketCategoryExample);
        //遍历条目，根据level分别放入两个list中
        ArrayList<MarketCategory> l1List = new ArrayList<>();
        ArrayList<MarketCategory> l2list = new ArrayList<>();
        for (MarketCategory category : categoryList) {
            if (category.getLevel().equals("L1")) {
                l1List.add(category);
            } else if (category.getLevel().equals("L2")) {
                l2list.add(category);
            }
        }
        //创建一个hashmap，对l2list进行遍历
        //根据二级条目的pid进行分类，放入map中
        HashMap<Integer, ArrayList<MarketCategory>> map = new HashMap<>();
        for (MarketCategory category : l2list) {
            Integer pid = category.getPid();
            ArrayList<MarketCategory> list;
            if (!map.containsKey(pid)) {
                list = new ArrayList<>();
                map.put(pid, list);
            } else {
                list = map.get(pid);
            }
            list.add(category);
        }

        //遍历一级类目
        for (MarketCategory l1Category : l1List) {
            CategoryVo l1CategoryVo = new CategoryVo(l1Category);
            List<MarketCategory> l2Categories = map.get(l1Category.getId());
            List<CategoryVo> children = new ArrayList<>();

            if (l2Categories == null) {
                l2Categories = new ArrayList<>();
            }
            for (MarketCategory l2Category : l2Categories) {
                CategoryVo l2CategoryVo = new CategoryVo(l2Category);
                children.add(l2CategoryVo);

            }
            l1CategoryVo.setChildren(children);
            categoryVoList.add(l1CategoryVo);
        }

        return categoryVoList;
    }
    @Override
    public List<LabelValueVo> l1() {
        MarketCategoryExample example = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = example.or();
        criteria.andLogicalDeleted(true);
        criteria.andLevelEqualTo("L1");
        List<MarketCategory> l1List = mapper.selectByExample(example);
        List<LabelValueVo> list = new ArrayList<>();
        for (MarketCategory category : l1List) {
            LabelValueVo labelValueVo = new LabelValueVo(category.getId(), category.getName());
            list.add(labelValueVo);
        }
        return list;
    }
    @Transactional
    @Override
    public int create(MarketCategory category) {
        int res = mapper.insert(category);
        return res;
    }

    @Override
    public int update(MarketCategory category) {
        category.setUpdateTime(LocalDateTime.now());
        // 更新
        int resCode = mapper.updateByPrimaryKeySelective(category);
        return resCode;
    }

    @Override
    public int update(CategoryVo categoryVo) {
        MarketCategory category = new MarketCategory();
        category.setId(categoryVo.getId());
        category.setName(categoryVo.getName());
        category.setDesc(categoryVo.getDesc());
        category.setIconUrl(categoryVo.getIconUrl());
        category.setPicUrl(categoryVo.getPicUrl());
        category.setLevel(categoryVo.getLevel());
        category.setKeywords(categoryVo.getKeywords());
        category.setPid(0);
        category.setUpdateTime(LocalDateTime.now());
        int resCode = mapper.updateByPrimaryKeySelective(category);
        return resCode;
    }

    @Override
    public int update(CategoryUpdateVo categoryUpdateVo) {
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String format = now.format(formatter);
        LocalDateTime updateTime = LocalDateTime.parse(format, formatter);
        MarketCategory marketCategory = new MarketCategory();
        marketCategory.setUpdateTime(updateTime);
        // 通过id修改信息
        marketCategory.setId(categoryUpdateVo.getId());
        marketCategory.setName(categoryUpdateVo.getName());
        marketCategory.setKeywords(categoryUpdateVo.getKeywords());
        marketCategory.setDesc(categoryUpdateVo.getDesc());
        marketCategory.setIconUrl(categoryUpdateVo.getIconUrl());
        marketCategory.setPicUrl(categoryUpdateVo.getPicUrl());
        marketCategory.setLevel(categoryUpdateVo.getLevel());
        marketCategory.setPid(categoryUpdateVo.getPid());
        int result = mapper.updateByPrimaryKeySelective(marketCategory);
        return result;
    }

    @Transactional
    @Override
    public int delete(int id) {
        MarketCategory marketCategory = new MarketCategory();
        marketCategory.setId(id);
        marketCategory.setDeleted(true);
        int result = mapper.updateByPrimaryKeySelective(marketCategory);
        return result;
    }
}
